package com.aishua.fire.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import javax.transaction.Transactional;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.aishua.fire.entity.addressBook.AddressBookInfo;
import com.aishua.fire.entity.organization.AddOrganizationInfo;
import com.aishua.fire.entity.organization.AddRoleInfo;
import com.aishua.fire.entity.organization.DeleteUserToOrganizationInfo;
import com.aishua.fire.entity.organization.OrganizationInfo;
import com.aishua.fire.entity.organization.OrganizationPostInfo;
import com.aishua.fire.entity.organization.UserToOrganizationInfo;
import com.aishua.fire.entity.selectrole.OrganizInfo;
import com.aishua.fire.entity.selectrole.RoleInfo;
import com.aishua.fire.entity.selectrole.SelectAddressBean;
import com.aishua.fire.entity.selectrole.SelectRoleBean;
import com.aishua.fire.mapper.AddressBookMapper;
import com.aishua.fire.mapper.OrganizationMapper;
import com.aishua.fire.service.OrganizationService;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service
public class OrganizationServiceImpl implements OrganizationService 
{
	
	@Autowired
	private OrganizationMapper organizationMapper;
	
    @Autowired
    private AddressBookMapper bookMapper;
	
	//生成随机数
	public static String getCard()
	{
		Random rand=new Random();//生成随机数
		String cardNnumer="";
		for(int a=0;a<9;a++)
		{
			cardNnumer+=rand.nextInt(10);//生成6位数字
		}
		log.info("增加组织结构部门信息 生成ID:{}",cardNnumer);

		return cardNnumer;
	}
	
	
	/**
	 * 增加组织结构部门信息
	 */
	@Transactional
	public boolean save(AddOrganizationInfo info) 
	{
		boolean flag = true;
		List<String> s_name = organizationMapper.findByUserName(info.getS_name());
		if (s_name.size() > 0) 
		{
			//代表该部门名称已经存在,无法继续添加
			return false;
		}
		if(StringUtils.isBlank(info.getF_id()))
		{
		    info.setF_id("222222");
		}
		//添加账户表
		organizationMapper.save(info);
		return flag;
	}
	
	/**
	 * 组织结构管理_查询组织结构信息列表
	 */
	@Override
	public List<OrganizationInfo> select(String id) 
	{
	    
	    
		Map<String, String> pa = new HashMap<String, String>();
		pa.put("type", id);
		List<OrganizationInfo> list = organizationMapper.select(pa);
		//如果是查询部门节点， 则将所有角色也一起传过去
		if("2".equals(id))
		{
		    //查询所有角色
		    List<OrganizationInfo> rolelist =  organizationMapper.getAllRole();
		    list.addAll(rolelist);
		}
		
		return list;
	}
	

	/**
	 * 组织结构管理_查询当前组织结构下所有员工
	 */
	@Override
	public List<AddressBookInfo> selectAddressBookInfoList(String id, String type) 
	{
	    List<AddressBookInfo> list = new ArrayList<AddressBookInfo>();
	    if("1".equals(type))
	    {
	        list = organizationMapper.selectAddressBookInfoList(id);
	    }
	    else
	    {
	        list = organizationMapper.getUserListByRoleId(id);
	    }
	    for(AddressBookInfo info : list)
	    {
            String rolestr = bookMapper.getRolesStr(info.getId());
            info.setOrganizationstr(rolestr);
	    }
		return list;
	}
	
	
	
	
	/**
	 * 修改组织结构 部门信息
	 */
	@Override
	public void update(AddOrganizationInfo info) 
	{
		List<String> s_name = organizationMapper.findByUserName(info.getS_name());
		if (s_name.size() > 1) 
		{
			//代表该部门名称已经存在,无法继续添加
			return;
		}
		organizationMapper.updateInfo(info);
	}

	/**
	 * 组织结构管理_删除部门信息
	 */
	@Override
	public void delete(String id) 
	{
		//删除当前节点
		organizationMapper.deleteOrganization(id);
		//删除当前节点下的用户关系
		organizationMapper.deleteUserAndOrganization(id);
	}

	/**
	 * 组织结构管理_新增岗位信息
	 */
	@Override
	public void savePost(OrganizationPostInfo info) 
	{
		// TODO Auto-generated method stub
		
	}

	/**
	 * 组织结构管理_添加当前通讯中某位员工
	 */
	@Override
	public void saveUserToOrganization(UserToOrganizationInfo info)
	{
		Map<String, String> pa = new HashMap<String, String>();
		
		//插入应急职责组织
		if("1".equals(info.getType()))
		{
		    for(String str : info.getUser_id())
            {
                pa.put("organ_id", info.getOrgan_id());
                pa.put("user_ids", str);
                organizationMapper.saveUserToOrganizationToMap(pa);
            }
		}
		//插入角色关联
		else
		{
		    for(String str : info.getUser_id())
            {
                pa.put("organ_id", info.getOrgan_id());
                pa.put("user_ids", str);
                organizationMapper.saveUserToRoleToMap(pa);
            }
		}
		
	}

	/**
	 * 组织结构管理_移除当前组织结构下某位员工
	 * @param info
	 */
	@Override
	public void deleteUserToOrganization(DeleteUserToOrganizationInfo info)
	{
		organizationMapper.deleteUserToOrganization(info);
	}

	/**
	 * 组织结构管理_查询不属于当前节点的通讯录列表
	 */
	@Override
	public List<AddressBookInfo> selectUserIsNotInOrganization(String id)
	{
		return organizationMapper.selectUserIsNotInOrganization(id);
	}


    @Override
    public boolean addRole(AddRoleInfo info) 
    {
        String id = getCard();
        Map<String, String> pa = new HashMap<String, String>();
        pa.put("rolr_name", info.getRolr_name());
        pa.put("o_id", info.getO_id());
        
        //校验角色名称
        List<String> s_name = organizationMapper.findRoleNameByName(pa);
        if (s_name.size() > 0) 
        {
            //代表该部门名称已经存在,无法继续添加
            return false;
        }
        info.setId(id);
        //插入部门和角色关联关系
        organizationMapper.addRoleAndOrgan(info);
        
        organizationMapper.addRole(info);
        
        return true;
    }

    /**
     * 组织结构管理_部门管理_根据部门节点查询角色列表信息
     */
    @Override
    public List<AddRoleInfo> selectRole(String o_id) 
    {
        return organizationMapper.selectRole(o_id);
    }

    /**
     * 部门组织角色下拉框
     */
    @Override
    public List<SelectRoleBean> roleSelect(String o_id) 
    {
        //查询当前节点的子节点
        List<SelectRoleBean> list = organizationMapper.selectOrgListByF_id(o_id);
        
        for(int i=0;i<list.size();i++)
        {
            SelectRoleBean beanA = list.get(i);
            
            SelectRoleBean bean = returnList(list.get(i).getId());
            bean.setId(beanA.getId());
            bean.setS_name(beanA.getS_name());
            
            Collections.replaceAll(list, list.get(i), bean); 
        }
        return list;
    }
    
    public SelectRoleBean returnList(String o_id)
    {
        
        SelectRoleBean bean = new SelectRoleBean();
        
        List<RoleInfo> roleList = organizationMapper.getRoleListByO_id(o_id);
        List<SelectRoleBean> orgList = organizationMapper.selectOrgListByF_id(o_id);
        
        for(int i=0;i<orgList.size();i++)
        {
            SelectRoleBean beanA = orgList.get(i);
            
            SelectRoleBean beans = returnList1(orgList.get(i).getId());
            beans.setId(beanA.getId());
            beans.setS_name(beanA.getS_name());
            Collections.replaceAll(orgList, orgList.get(i), beans);
        }
        bean.setRoleList(roleList);
        bean.setOrgList(orgList);
        
        return bean;
    }
    
    public SelectRoleBean returnList1(String o_id)
    {
        
        SelectRoleBean bean = new SelectRoleBean();
        
        List<RoleInfo> roleList = organizationMapper.getRoleListByO_id(o_id);
        List<SelectRoleBean> orgList = organizationMapper.selectOrgListByF_id(o_id);
        
        for(int i=0;i<orgList.size();i++)
        {
            
            SelectRoleBean beanA = orgList.get(i);
             SelectRoleBean beans = returnList1(orgList.get(i).getId());

            beans.setId(beanA.getId());
            beans.setS_name(beanA.getS_name());
            Collections.replaceAll(orgList, orgList.get(i), beans);
        }
        bean.setRoleList(roleList);
        bean.setOrgList(orgList);
        
        return bean;
    }
    public SelectRoleBean returnList2(String o_id)
    {
        
        SelectRoleBean bean = new SelectRoleBean();
        
        List<RoleInfo> roleList = organizationMapper.getRoleListByO_id(o_id);
        List<SelectRoleBean> orgList = organizationMapper.selectOrgListByF_id(o_id);
        
        for(int i=0;i<orgList.size();i++)
        {
            
            SelectRoleBean beanA = orgList.get(i);
            SelectRoleBean beans = returnList1(orgList.get(i).getId());

            beans.setId(beanA.getId());
            beans.setS_name(beanA.getS_name());
            Collections.replaceAll(orgList, orgList.get(i), beans);
        }
        
        bean.setRoleList(roleList);
        bean.setOrgList(orgList);
        return bean;
    }
    
    public SelectRoleBean returnList3(String o_id)
    {
        
        SelectRoleBean bean = new SelectRoleBean();
        
        List<RoleInfo> roleList = organizationMapper.getRoleListByO_id(o_id);
        List<SelectRoleBean> orgList = organizationMapper.selectOrgListByF_id(o_id);
        bean.setRoleList(roleList);
        bean.setOrgList(orgList);
        return bean;
    }

    /**
     * 职责组织通讯录树形结构
     */
    @Override
    public List<SelectRoleBean> addressBookSelect(String o_id) 
    {
      //查询当前节点的子节点
        List<SelectRoleBean> list = organizationMapper.selectOrgListByF_id(o_id);
        
        for(int i=0;i<list.size();i++)
        {
            SelectRoleBean beanA = list.get(i);
            
            SelectRoleBean bean = returnList_A(list.get(i).getId());
            bean.setId(beanA.getId());
            bean.setS_name(beanA.getS_name());
            
            Collections.replaceAll(list, list.get(i), bean); 
        }
        return list;
    }
    
    public SelectRoleBean returnList_A(String o_id)
    {
        
        SelectRoleBean bean = new SelectRoleBean();
        
        List<RoleInfo> roleList = organizationMapper.getAddressListByO_id(o_id);
        List<SelectRoleBean> orgList = organizationMapper.selectOrgListByF_id(o_id);
        
        for(int i=0;i<orgList.size();i++)
        {
            SelectRoleBean beanA = orgList.get(i);
            
            SelectRoleBean beans = returnList1_A(orgList.get(i).getId());
            beans.setId(beanA.getId());
            beans.setS_name(beanA.getS_name());
            Collections.replaceAll(orgList, orgList.get(i), beans);
        }
        bean.setRoleList(roleList);
        bean.setOrgList(orgList);
        
        return bean;
    }
    
    public SelectRoleBean returnList1_A(String o_id)
    {
        
        SelectRoleBean bean = new SelectRoleBean();
        
        List<RoleInfo> roleList = organizationMapper.getAddressListByO_id(o_id);
        List<SelectRoleBean> orgList = organizationMapper.selectOrgListByF_id(o_id);
        
        for(int i=0;i<orgList.size();i++)
        {
            
            SelectRoleBean beanA = orgList.get(i);
             SelectRoleBean beans = returnList2_A(orgList.get(i).getId());

            beans.setId(beanA.getId());
            beans.setS_name(beanA.getS_name());
            Collections.replaceAll(orgList, orgList.get(i), beans);
        }
        bean.setRoleList(roleList);
        bean.setOrgList(orgList);
        
        return bean;
    }
    public SelectRoleBean returnList2_A(String o_id)
    {
        
        SelectRoleBean bean = new SelectRoleBean();
        
        List<RoleInfo> roleList = organizationMapper.getAddressListByO_id(o_id);
        List<SelectRoleBean> orgList = organizationMapper.selectOrgListByF_id(o_id);
        
        for(int i=0;i<orgList.size();i++)
        {
            
            SelectRoleBean beanA = orgList.get(i);
            SelectRoleBean beans = returnList3_A(orgList.get(i).getId());

            beans.setId(beanA.getId());
            beans.setS_name(beanA.getS_name());
            Collections.replaceAll(orgList, orgList.get(i), beans);
        }
        
        bean.setRoleList(roleList);
        bean.setOrgList(orgList);
        return bean;
    }
    
    public SelectRoleBean returnList3_A(String o_id)
    {
        
        SelectRoleBean bean = new SelectRoleBean();
        
        List<RoleInfo> roleList = organizationMapper.getAddressListByO_id(o_id);
        List<SelectRoleBean> orgList = organizationMapper.selectOrgListByF_id(o_id);
        
        for(int i=0;i<orgList.size();i++)
        {
            
            SelectRoleBean beanA = orgList.get(i);
            SelectRoleBean beans = returnList4_A(orgList.get(i).getId());

            beans.setId(beanA.getId());
            beans.setS_name(beanA.getS_name());
            Collections.replaceAll(orgList, orgList.get(i), beans);
        }
        
        bean.setRoleList(roleList);
        bean.setOrgList(orgList);
        return bean;
    }
    
    public SelectRoleBean returnList4_A(String o_id)
    {
        
        SelectRoleBean bean = new SelectRoleBean();
        
        List<RoleInfo> roleList = organizationMapper.getAddressListByO_id(o_id);
        List<SelectRoleBean> orgList = organizationMapper.selectOrgListByF_id(o_id);
        bean.setRoleList(roleList);
        bean.setOrgList(orgList);
        return bean;
    }


	@Override
	public List<String> getr_idWitha_id(String user_id) {
		return organizationMapper.getr_idWitha_id(user_id);
	}
}
